/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package net.tygzs.minerpg.Configuration;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.tygzs.minerpg.Configuration.Team.TeamInfo;
import net.tygzs.minerpg.Main;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;

/**
 *
 * @author thtTNT
 */
public class TeamConfig
{

    public List<TeamInfo> teams = new ArrayList<>();
    public Scoreboard sb;

    public TeamConfig()
    {
    	/*这段代码我在重写，是弄在teams内的文件中的所有文件夹*/
        try
        {
            sb = Bukkit.getScoreboardManager().getMainScoreboard();
            clean();
            File files[] = new File(Main.getPlugin().getDataFolder()+"teams".listfiles());
        	for(File file:files){


            FileConfiguration config = YamlConfiguration.loadConfiguration(file);
            Set<String> names = config.getKeys(false);
            for (String name : names)
            {
                if (sb.getTeam(name) != null)
                {
                    Main.getPlugin().getLogger().info("阵营系统和主系统发生冲突," + name + "阵营未被加载");
                    continue;
                }
                Team team = sb.registerNewTeam(name);
                for (String PlayerTeam : config.getStringList(name + ".mumbers"))
                {
                    team.addPlayer(Bukkit.getOfflinePlayer(PlayerTeam));
                }
                if (config.contains(name + ".perfix"))
                {
                    team.setPrefix(config.getString(name + ".perfix"));
                    Bukkit.broadcastMessage(config.getString(name + ".perfix"));
                    Bukkit.broadcastMessage(team.getPrefix());
                }
                if (config.contains(name + "suffix"))
                {
                    team.setSuffix(config.getString(name + ".suffix"));
                }
                teams.add(new TeamInfo(team));
            }
        } catch (IOException ex)
        {
            Logger.getLogger(TeamConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
    }

    public void save()
    {
        try
        {
            File files[] = new File(Main.getPlugin().getDataFolder()+"teams");
            FileConfiguration config = YamlConfiguration.loadConfiguration(file);
        	for(File file:files){

            for (TeamInfo ti : teams)
            {
                List<String> mumbers = new ArrayList<>();
                for (OfflinePlayer player : ti.getMumber())
                {
                    mumbers.add(player.getName());
                }
                config.set(ti.getName() + ".mumbers", mumbers);
                config.set(ti.getName() + ".perfix", ti.getPerfix());
                config.set(ti.getName() + ".suffix", ti.getSuffix());
            }
            config.save(file);
            clean();
        }
        } catch (IOException ex)
        {
            Logger.getLogger(TeamConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getBelong(String name)
    {
        Team team = sb.getPlayerTeam(Bukkit.getOfflinePlayer(name));
        if (team == null)
        {
            return null;
        } else
        {
            return team.getName();
        }
    }
    public static createTeam(String name)
    {
    	teams.add();
    }
    public TeamInfo getTeam(String name)
    {
        int i = 0;
        for (TeamInfo ti : teams)
        {
            if (ti.getName().equals(name))
            {
                return teams.get(i);
            }
            i++;
        }
        return null;
    }

    public boolean Join(String PlayerName, String TeamName)
    {
        exit(PlayerName);
        if (getTeam(TeamName) == null)
        {
            return false;
        }
        getTeam(TeamName).addPlayer(PlayerName);
        return true;
    }

    public boolean exit(String PlayerName)
    {
        if (getBelong(PlayerName) == null)
        {
            return false;
        }
        getTeam(getBelong(PlayerName)).removePlayer(PlayerName);
        return true;
    }

    private void clean()
    {
        for (TeamInfo ti : teams)
        {
            ti.unRegister();
        }
        teams.clear();
    }
}
